package com.example.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class DBOpenHelper extends SQLiteOpenHelper {

    private SQLiteDatabase db;

    public DBOpenHelper(Context context, int version) {
        super(context, "db",null, version);
        db = getReadableDatabase();
    }

    private static final String DB_TABLE = "student";
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_CLASS = "clas";
    private static final String KEY_NUMBER = "number";

    private static final String DB_CREATE = "create table " + DB_TABLE
            + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_NAME +
            " text not null, " + KEY_CLASS + " text not null," + KEY_NUMBER +
            " text not null);";

    @Override
    public void onCreate(SQLiteDatabase _db) {
        _db.execSQL(DB_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
    }


    //增加新数据
    public long insert(Student student) {
        ContentValues newValues = new ContentValues();
        newValues.put(KEY_NAME, student.Name);
        newValues.put(KEY_CLASS, student.Clas);
        newValues.put(KEY_NUMBER, student.Number);
        return db.insert(DB_TABLE, null, newValues);
    }

    //查询数据
    public Student[] queryAllData() {
        Cursor results = db.query(DB_TABLE, new String[]{KEY_ID, KEY_NAME, KEY_CLASS, KEY_NUMBER}, null, null, null, null, null);
        return ConvertToStudent(results);
    }

    private Student[] ConvertToStudent(Cursor results) {
        List<Student> students = new ArrayList<>();
        while (results.moveToNext()) {
            Student student = new Student();
            student.ID = results.getInt(0);
            student.Name = results.getString(1);
            student.Clas = results.getString(2);
            student.Number = results.getString(3);
            students.add(student);
        }
        return students.toArray(new Student[0]);
    }

    public Student[] queryOneData(long id) {
        Cursor results = db.query(DB_TABLE, new String[]{KEY_ID, KEY_NAME, KEY_CLASS, KEY_NUMBER}, KEY_ID + "=" + id, null, null, null, null);
        return ConvertToStudent(results);
    }

    //删除数据
    public long deleteAllData() {
        return db.delete(DB_TABLE, null, null);
    }

    public long deleteOneData(int id) {
        return db.delete(DB_TABLE, KEY_ID + "=" + id, null);
    }

    //更新数据
    public long updateOneData(long id, Student student) {
        ContentValues updateValues = new ContentValues();
        updateValues.put(KEY_NAME, student.Name);
        updateValues.put(KEY_CLASS, student.Clas);
        updateValues.put(KEY_NUMBER, student.Number);
        return db.update(DB_TABLE, updateValues, KEY_ID + "=" + id, null);
    }

}
